package com.proton.carepatchtemp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.PrfChooseAdapter;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.JSONUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ChooseProfileActivity extends BaseActivity {

    List<Profile> mProfiles = new ArrayList<>();
    private ImageButton mBtnBack;
    private RecyclerView mRcvProfileList;
    /**
     * 新增档案
     */
    private TextView mTvAddProfile;
    private PrfChooseAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    @Override
    public void dealEvent(MainEvent event) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_profile);
        initView();

        mAdapter = new PrfChooseAdapter(ChooseProfileActivity.this, mProfiles);
        mLinearLayoutManager = new LinearLayoutManager(ChooseProfileActivity.this, LinearLayoutManager.VERTICAL, false);
        mRcvProfileList.setLayoutManager(mLinearLayoutManager);
        mRcvProfileList.setAdapter(mAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        String msg = VCare.getACacheInstance().getAsString(Extras.ACHCHE_PROFILE);
        if (!TextUtils.isEmpty(msg)) {
            Type listType = new TypeToken<ArrayList<Profile>>() {
            }.getType();
            List<Profile> profiles = JSONUtils.getObj(msg, listType, "yyyy-MM-dd");
            //LogUtils.e(profiles);
            if (profiles.size() > 0) {
                mProfiles.clear();
                mProfiles.addAll(profiles);
                mRcvProfileList.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void setListener() {

        mBtnBack.setOnClickListener(view -> {
            finish();
        });

        mTvAddProfile.setOnClickListener(view -> {
            Intent goAddPrf = new Intent(ChooseProfileActivity.this, AddProfileActivity.class);
            startActivity(goAddPrf);
        });

    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void initView() {
        mBtnBack = findViewById(R.id.btn_back);
        mRcvProfileList = findViewById(R.id.rcv_profile_list);
        mTvAddProfile = findViewById(R.id.tv_add_profile);
    }
}
